<script setup lang="ts">
import {onMounted, reactive, ref} from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import {getCaptcha,login} from '@/api/auth'
import {success} from '@/api/msg.js';
import {ElMessage, ElNotification} from "element-plus";
/*export default defineComponent({
  setup(){
    const centerDialogVisible = ref(false)
    const openDialog = () => {
      console.log("123")
      centerDialogVisible.value = true;
    }
    return {openDialog,centerDialogVisible};
  }
})*/

const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  username: '',
  password:'',
  code:'',
  key:'',
})

const centerDialogVisible = ref(false)
const openDialog = () => {
  resetForm();
  get();
  centerDialogVisible.value = true;
}

const resetForm = () => {
  if (ruleFormRef.value){
    ruleFormRef.value.resetFields()
  }
}

const rules = reactive<FormRules>({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 2, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '验证码不能为空', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {

      login(ruleForm).then((res:any)=>{
        success(res.msg);
      })

    } else {
      console.log('error submit!', fields)
    }
  })
}

const imgBase = ref<string>(null);

const get =()=>{
  getCaptcha().then((res:any)=>{
    ruleForm.key = res.data.key;
    imgBase.value = res.data.imgBase;
  })
}

defineExpose({
  openDialog,
})
</script>

<template>
  <div>
    <el-dialog
        v-model="centerDialogVisible"
        title="欢迎来到登录页面(Welcome To Login)"
        align-center
        :close-on-click-modal="false"
        class="el-dialog"
        width="30%"
        @close="resetForm"
    >
      <el-form
          label-position="left"
          label-width="100px"
          :model="ruleForm"
          style="max-width: 460px"
          ref="ruleFormRef"
          :rules="rules"
          :size="formSize"
      >
        <el-form-item label="用户名(username)" prop="username">
          <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="密码(password)" prop="password">
          <el-input v-model="ruleForm.password" />
        </el-form-item>
        <el-form-item label="验证码(captcha)" prop="code">
          <el-input v-model="ruleForm.code" class="input-box"/>
          <el-image class="img-box" :src="imgBase" @click="get"/>
        </el-form-item>
        <el-form-item>
          <el-button @click="submitForm(ruleFormRef)">登录</el-button>
          <el-button>注册</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
.input-box{
  width: 130px;
}
.img-box{
  width: 130px;
  height: 32px;
}
@media screen and (max-width: 600px){
  /deep/.el-dialog{
    width: 100% !important;
  }
}
/deep/.el-form-item__label{
  width: 150px!important;
}
</style>